import SimpleHTTPServer
import SocketServer
import logging
import cgi
import sys
from BooleanSearch import BooleanSearch

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    BooleanSearch = BooleanSearch()
    
    def do_GET(self):
        logging.warning("======= GET STARTED =======")
        logging.warning(self.headers)
        logging.warning(self.request)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.warning("======= POST STARTED =======")
        logging.warning(self.headers)
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })
        logging.warning("======= POST VALUES =======")
        self.BooleanSearch.executeSearch(form.getvalue("searchValue"))
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

if __name__ == "__main__":
        if len(sys.argv) > 2:
            PORT = int(sys.argv[2])
            I = sys.argv[1]
        elif len(sys.argv) > 1:
            PORT = int(sys.argv[1])
            I = ""
        else:
            PORT = 8000
            I = ""
            Handler = ServerHandler
        httpd = SocketServer.TCPServer(("", PORT), Handler)
        print "@rochacbruno Python http server version 0.1 (for testing purposes only)"
        print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT)
        httpd.serve_forever()
